class Solution {
public:
    string decodeString(string s) {
        stack<int> math;
        stack<string> eng;
        int num = 0;
        string tmp = "";
        string ret;
        for(int i = 0;i<s.size();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                num = num*10+s[i]-'0';
            }
            else if(s[i] == '[')
            {
                //记录数字和字符串
                math.push(num);
                num = 0;
                eng.push(tmp);
                tmp = "";
            }
            else if(s[i] == ']')
            {
                //开始构造字符串
                int cnt = math.top();
                math.pop();
                for(int i = 0;i<cnt;i++)
                {
                    eng.top()+=tmp;
                }
                tmp = eng.top();
                eng.pop();
            }
            else
            {
                //普通字符
                tmp+=s[i];
            }

        }
        return tmp;
    }
};